<?php
/**
 * Created by PhpStorm.
 * User: luohuaming
 * Date: 2016/7/27
 * Time: 20:37
 */

namespace Admin\Controller;


class IndexController extends BaseController
{
    protected function _initialize()
    {
//        parent::_initialize();
        $this->bcItemPush('服务器信息', U('Index/index'));
    }
    public function index(){
        $con = mysqli_connect(C('DB_HOST'), C('DB_USER'), C('DB_PWD'));	//root链接数据库来提权读取版本信息
        $info = array(
            'ThinkPHP版本'	=>	THINK_VERSION,
            '操作系统'		=>	PHP_OS,
            '服务器环境'		=>	$_SERVER["SERVER_SOFTWARE"],
            'PHP环境'		=>	PHP_VERSION.'/'.php_sapi_name(),
            'MySQL版本'		=>	mysqli_get_client_version($con),
            '主机信息'		=>	$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].' '.$_SERVER['SERVER_PROTOCOL'],
            'WEB目录'		=>	$_SERVER["DOCUMENT_ROOT"],
            '服务器域名/IP'	=>	$_SERVER['SERVER_NAME'].' [ '.gethostbyname($_SERVER['SERVER_NAME']).' ]',
            '服务器时间'	=>	date("Y年n月j日 H:i:s"),
            '北京时间'		=>	gmdate("Y年n月j日 H:i:s",time()+8*3600),
            '执行时间限制'	=>	ini_get('max_execution_time').'秒',
            '上传附件限制'	=>	ini_get('upload_max_filesize'),
            '剩余空间'		=>	round((disk_free_space(".")/(1024*1024)),2).'M',
        );
        $this->assign('info', $info);
        $this->display();
    }

    /**
     * Ajax获取用户菜单列表
     */
    public function menuList(){
        $user = session('admin');
        $PrivilegeModel = D('Privilege');
        $privilegeList = $PrivilegeModel->getPrivilegeMenuListByAdmin($user['id']);
        $this->ajaxReturn($privilegeList);
    }

    public function modifyPasswordUI(){
        $this->bcItemPush('修改密码', U('Index/modifyPasswordUI'));
        $this->display("modifyPassword");
    }

    public function modifyPassword(){
        $admin = $this->getAdmin();
        $password = I('password');
        $newPassword = I('newPassword');
        $rePassword = I('rePassword');
        if(!isset($password)){
            $this->ajaxReturn(array('success'=>false,'message'=>'请输入原始密码！'));
        }
        if(!isset($newPassword)){
            $this->ajaxReturn(array('success'=>false,'message'=>'请输入新密码！'));
        }
        if(strlen($newPassword) < 6 || strlen($newPassword) > 18){
            $this->ajaxReturn(array('success'=>false,'message'=>'新密码必须在6-18位之间！'));
        }
        if($rePassword != $newPassword){
            $this->ajaxReturn(array('success'=>false,'message'=>'新密码与确认密码不一致！'));
        }
        $password = md5($password . C('MD5_KEY'));
        if($admin['password'] != $password){
            $this->ajaxReturn(array('success'=>false,'message'=>'原始密码不正确！'));
        }
        $newPassword = md5($newPassword . C('MD5_KEY'));
        D('Admin')->where(array('id'=>$admin['id']))->setField('password',$newPassword);
        $this->ajaxReturn(array('success'=>true,'message'=>'密码修改成功！'));
    }

}